package com.dao;

import java.util.HashMap;
import java.util.List;

import org.springframework.dao.support.DaoSupport;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.ibatis.sqlmap.engine.transaction.jdbc.JdbcTransaction;
import com.model.BoardBean;

public class BoardDAO {

	private SqlMapClientTemplate dsBatis;

	public void setDsBatis(SqlMapClientTemplate dsBatis) {
		this.dsBatis = dsBatis;
		System.out.println("setDsBatis" + dsBatis);
	}

	public int searchTotalBoard(HashMap<String, String> hm){
		return (int) dsBatis.queryForObject("searchTotalBBS", hm);
	}
	
	public int totalBoard(){
		return (int) dsBatis.queryForObject("totalBBS");
	}

	HashMap<String, Integer> pageInfo = new HashMap<String, Integer>();
	public List<BoardBean> listBoard(int start, int countPerPage) {
		System.out.printf("[DAO] listBoard select call start=%d, countPerPage=%d\n", start, countPerPage);
		pageInfo.put("start", start);
		pageInfo.put("countPerPage", countPerPage);

		//return dsBatis.queryForList("selectAll", pageInfo);
		//dsBatis.queryForList(statementName, skipResults, maxResults)
		//dsBatis.queryForList(statementName, parameterObject, skipResults, maxResults)
		return dsBatis.queryForList("selectAll", start, countPerPage);
	}

	public List<BoardBean> detailBoard(int no) {
		return this.dsBatis.queryForList("selectDetail", no);
	}

	public void insertBoard(BoardBean bb) {
		// TODO Auto-generated method stub
		this.dsBatis.insert("insertBBS", bb);
	}

	public String updateBoard(BoardBean bb) {
		// TODO Auto-generated method stub
		System.out.println("[DAO] updateBoard call "+bb);
		int result = this.dsBatis.update("updateBBS", bb);
		if( result == 1){
			return "success"; 
		}else{
			return "fail";
		}

	}

	public void hitCnt(int no) {
		// TODO Auto-generated method stub
		this.dsBatis.update("hitCntBBS", no);
	}
	
	public void deleteBoard(int no){
		this.dsBatis.delete("deleteBBS", no);
	}

	public List<BoardBean> search(HashMap<String, String> hm, int start, int countPerPage) {
		// TODO Auto-generated method stub
		//return this.dsBatis.queryForList("searchBBS", hm);
		return this.dsBatis.queryForList("searchBBS", hm, start, countPerPage);
	}
}
